unit PlayerUnit;

interface

uses common;

type
  Player = class
    function move(height, lastNumber, sortedNumber : integer; arr : array of integer) : integer;
  end;


implementation

{ Player }

function Player.move(height, lastNumber, sortedNumber : integer; arr : array of integer): integer;
var
  i, j, max, numberMax, ran: Integer;
  flag, ok, ruin: Boolean;
begin
  Randomize;
  ran:=Random (100);
  flag:=False;
  ok:=False;
  max:=0;
  for i:=0 to N-1-sortedNumber do
  begin
    if arr[i] > max then
    begin
      max:=arr[i];
      numberMax:=i;
    end;
  end;
  if height >= max then
  begin
    move:= HIT(Random(N-sortedNumber));
    flag:=True;
  end else begin
    for i:=0 to N-1-sortedNumber do
    begin
      if (arr[i] < height) then
      begin
        if ((lastNumber + 4 < i) or (lastNumber > i)) and ((4 - N-1-sortedNumber - lastNumber) < i) then
        begin
          move:= HIT(i);
          flag:=True;
        end else begin
          if i > numberMax then
          begin
            move:= HIT(i-1);
            flag:=True;
          end else begin
            for j:=0 to i do
            begin
              if arr[j] > arr[i] then
              begin
                ok:= True;
              end;
            end;
            if ok = true then
            begin
              move:= HIT(i-1);
              flag:=True;
            end;
          end;
        end;
      end;
    end;
  end;
  if flag = false then
  begin
    move:=HIT (lastNumber + 4);
  end;
  if (lastNumber >= 20) and (lastNumber <= N-1-sortedNumber - 4) and (ran < 99) then
  begin
    move:=HIT (lastNumber + 4);
  end;
end;

end.
